using System;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TXLooker.Common.Core;
using TXLooker.WorkItemExtention.Core;
using TXLooker.WorkItemExtention.Properties;

namespace TXLooker.WorkItemExtention.UI
{
    public partial class GoToWorkItemForm : Form
    {
        private IOlWorkItemProcessor m_servers;
        private GoToWorkItemForm()
        {
            InitializeComponent();
        }

        public GoToWorkItemForm(IOlWorkItemProcessor workItemProcessor)
            : this()
        {
            this.OlWorkItemProcessor = workItemProcessor;

            this.m_comboServers.DisplayMember = "ServerName";
            this.m_comboServers.DataSource = this.OlWorkItemProcessor.Servers;
            //foreach (IServerProvider server in this.OlWorkItemProcessor.Servers)
            //    this.m_comboServers.Items.Add(server.ServerName);

            if (this.m_comboServers.Items.Count > 0)
                this.m_comboServers.SelectedIndex = 0;
  
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.m_txtID.Focus();
        }

        public IOlWorkItemProcessor OlWorkItemProcessor
        {
            get { return m_servers; }
            set
            {
                if (object.ReferenceEquals(value, null))
                    throw new NullReferenceException();
                m_servers = value;
            }
        }

        private void OnOkClick(object sender, EventArgs e)
        {
            if (this.m_comboServers.Text.Equals(string.Empty))
            {
                MessageBox.Show(this,Resources.STRING_SELECT_VALID_SERVER,Resources.EXTENTION_NAME,MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            
            if (this.m_txtID.Text.Equals(string.Empty) || this.m_txtID.Text.Contains(" "))
            {
                MessageBox.Show(this, Resources.STRING_PLEASE_ENTER_VALID_ID, Resources.EXTENTION_NAME, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Cursor oldCur = this.Cursor;
            try
            {
                this.Cursor = Cursors.WaitCursor;
                TeamFoundationServer tfs = null;
                //foreach (IServerProvider server in this.OlWorkItemProcessor.Servers)
                //{
                //    if (server.ServerName == this.m_comboServers.Text)
                //        tfs = server.Server;
                //}
                tfs = (this.m_comboServers.SelectedItem as IServerProvider).Server;

                if (tfs == null)
                {
                    this.Cursor = oldCur;
                    return;
                }

                try
                {
                    WorkItemStore store = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
                    WorkItem wiItem = store.GetWorkItem(int.Parse(this.m_txtID.Text));
                    if (wiItem == null)
                    {
                        MessageBox.Show(this, Resources.STRING_THE_WORKITEM_DOES_NOT_EXIST, Resources.EXTENTION_NAME, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    try
                    {
                        this.OlWorkItemProcessor.OlWorkItemChange(wiItem);
                        this.Close();
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show(this,ex.Message,Resources.EXTENTION_NAME,MessageBoxButtons.OK,MessageBoxIcon.Error);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, Resources.EXTENTION_NAME, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            finally
            {
                this.Cursor = oldCur;
            }
            
        }
    }
}